package cn.audit.controller.zqmk;

import cn.audit.bean.pojo.AuditPlan;
import cn.audit.bean.pojo.Message;
import cn.audit.bean.pojo.User;
import cn.audit.repository.operation.AuditPlanRepository;
import cn.audit.repository.user.UserRepository;
import cn.audit.repository.zqmk.MessageRepository;
import cn.audit.service.approval.ApprovalService;
import cn.audit.service.operation.AuditPlanService;
import cn.audit.service.zqmk.MessageService;
import cn.audit.util.PagerUtil;
import cn.audit.util.Paging;
import cn.audit.bean.dto.zuzhangzhengshuai;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 钟琪
 * @Date: 2019/5/28 14:51
 */

@Controller
@Transactional
public class MyPlatformController {
    @Resource
    private AuditPlanRepository auditPlanRepository;
    @Resource
    private AuditPlanService auditPlanService;

    @Resource
    private UserRepository userRepository;

    @Resource
    private ApprovalService approvalService;

    @Resource
    private MessageService messageService;

    @Resource
    private MessageRepository messageRepository;

    @RequestMapping("/loupanchart")
    public String loupanchart(){
        return "/workspce/loupanchart";
    }

    @RequestMapping("/plan")
    public String plan(Model model){
       List<zuzhangzhengshuai> auditPlanList= auditPlanService.bbb();
       model.addAttribute("auditPlanList",auditPlanList);
        return "/workspce/loupanchart";
    }

    @RequestMapping("/auditPlanByDel")
    public String auditPlanByDel(Long id){
        auditPlanRepository.deleteById(id);
        return "/workspce/loupanchart";
    }

    @ResponseBody
    @RequestMapping("/auditPlanUpdate")
    public zuzhangzhengshuai auditPlanUpdate(Long id){
       zuzhangzhengshuai zuzhangzhengshuai= auditPlanService.findId(id);
        System.out.println(zuzhangzhengshuai+"刷刷");
        return zuzhangzhengshuai;
    }

    @ResponseBody
    @RequestMapping("/auupd")
    public zuzhangzhengshuai auupd(AuditPlan auditPlan, String date, String username){
        System.out.println(auditPlan.toString());
        System.out.println(date+username);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date1 = null;

       String dateInString=date;
        try {
            date1 = formatter.parse(dateInString);

            /* System.out.println(formatter.format(date)); */
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date1);
        Optional<AuditPlan> optionalAuditPlan=auditPlanRepository.findById(auditPlan.getId());
        AuditPlan a=optionalAuditPlan.get();
        auditPlan.setCreationdate(date1);
        auditPlan.setInstitutions(a.getInstitutions());
        auditPlan.setIsyear(a.getIsyear());
        auditPlan.setStatus(a.getStatus());
        auditPlan.setType(a.getType());
        auditPlan.setYear(a.getYear());
        auditPlanRepository.save(auditPlan);
        Optional<User> optionalUser=userRepository.findById(auditPlan.getCreationname());
        User user=optionalUser.get();
        user.setName(username);
        userRepository.save(user);
        zuzhangzhengshuai zuzhangzhengshuai= auditPlanService.findId(auditPlan.getId());




        return zuzhangzhengshuai;
    }


    @RequestMapping("/Processed")
    public String Processed(@RequestParam(value = "page",defaultValue = "1") Integer page,Model model){


        List<zuzhangzhengshuai> list= approvalService.eee();
        System.out.println(list);
        PagerUtil pu=Paging.findBook(page,list);
        System.out.println(list);

       model.addAttribute("pu",pu);
        return "/workspce/Processed";
    }

    @RequestMapping("/MyMessge")
    public String MyMessge(@RequestParam(value = "page",defaultValue = "1") Integer page,Model model){
        List<zuzhangzhengshuai> list= messageService.selsectByAll();
        System.out.println(list);
        PagerUtil pu=Paging.findBook(page,list);
        System.out.println(list);

        model.addAttribute("pu",pu);
        return "/workspce/MyMessge";
    }

    @RequestMapping("/messagedel")
    public String messagedel(Integer id){
        messageRepository.deleteById(id);
        return "redirect:/MyMessge";
    }
    @ResponseBody
    @RequestMapping("/messageread")
    public String messageread(Integer id){
        Optional<Message> optionalMessage=messageRepository.findById(id);
        Message message=optionalMessage.get();
        System.out.println(message+"111111111111111111111111111111111111111");
        message.setIsread(1);


        messageRepository.save(message);
        return "已阅读";
    }


}
